import axios from "axios";
import { MessageBox, Message } from "element-ui";
import store from "@/store";
import { getToken } from "@/utils/auth";

// 创建axiOS实例
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // 配置基准地址
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
});

// 请求拦截器
service.interceptors.request.use(config => {
  // 一定要return config 否则，请求会被放弃
  // console.log(config);
  const token = getToken()
  if (token) config.headers['Authorization'] = `Bearer ${token}`
  return config
}, error => {
  // 抛出错误
  return Promise.reject(error)
})

// 相应拦截器(结构返回数据，集中处理错误)
service.interceptors.response.use(response => {
  // console.log(response);
  return response.data;
}, error => {
  // 请求出错，抛出错误
  if (response.status == 500) {
    return Message.warning('无此数据')
  }
  return Promise.reject(new Error(error))

})

export default service;
